package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.acticle.domain.Query;
import cn.wolfcode.luowowo.acticle.domain.QueryContent;
import cn.wolfcode.luowowo.acticle.query.QuerytionsQuery;
import cn.wolfcode.luowowo.acticle.service.IQueryService;
import cn.wolfcode.luowowo.article.mapper.QueryContentMapper;
import cn.wolfcode.luowowo.article.mapper.QueryMapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import java.util.Date;
import java.util.List;

@Service
public class QueryServiceImpl implements IQueryService{

    @Autowired
    private QueryMapper queryMapper;

    @Autowired
    private QueryContentMapper queryContentMapper;

    @Override
    public void saveQuery(Query query) {
        //summary
        String content = query.getQueryContent().getContent();
        if(content.length() > 80){
            query.setSummary(content.substring(0,80));
        }else{
            query.setSummary(content);
        }

        //createTime
        query.setCreateTime(new Date());
        query.setViewnum(0);
        query.setFollownum(0);
        query.setReplynum(0);
        queryMapper.insert(query);

        QueryContent queryContent = new QueryContent();
        queryContent.setId(query.getId());
        queryContent.setContent(content);
        queryContentMapper.insert(queryContent);
    }

    @Override
    public PageInfo queryForList(QuerytionsQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        return new PageInfo(queryMapper.selectForList(qo));
    }

    @Override
    public Query getQueryByid(Long id) {
        Query query = queryMapper.selectByPrimaryKey(id);
        return query;
    }

    @Override
    public QueryContent getContent(Long id) {
        return queryContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public int getReplay(Long id) {
        return queryMapper.selectByReplay(id);
    }

    @Override
    public void setReply(Long id, int newreply) {
        queryMapper.updateByReply(id,newreply);
    }

}
